package jlfm.lastfm.apps.recorder;

import java.io.File;

import jlfm.lastfm.client.LastFmClientFaultException;
import jlfm.lastfm.client.TrackMetadata;

public class BaseDirFileNameProvider implements FileNameProvider {

	private String baseDir;

	public String getBaseDir() {
		return baseDir;
	}

	public BaseDirFileNameProvider(String baseDir) {
		this.baseDir = baseDir;
	}

	public File getOutputFile(TrackMetadata md) {
		File dirs = new File(baseDir
				+ "/"
				+ md.getArtist().replace("/", " ")
				+ "/"
				+ ((md.getAlbum() != null) ? md.getAlbum().replace("/", " ")
						: ""));

		if (!dirs.exists() && !dirs.mkdirs()) {
			throw new LastFmClientFaultException(
					"Could not create directories for path: " + dirs);
		}
		File outputFile = new File(dirs + "/" + md.getTrack().replace("/", " ")
				+ ".mp3");
		return outputFile;

	}

}
